#include <iostream>
using namespace std;
struct Node {
	int val;
	Node* next;
	Node(int _val): val(_val), next(nullptr) {}
};
Node* createList(int[],int);
int getKthFromEnd(Node*,int);
int main(){
	int nums[5]={1,2,3,4,5};
	Node* l=createList(nums,5);
	cout<<getKthFromEnd(l,5)<<endl;
	return 0;
}
Node* createList(int values[], int size) {
	Node* ret = new Node(values[0]);
	Node* p = ret;
	for (int i = 1; i < size; ++i) {
		p->next = new Node(values[i]);
		p = p->next;
	}
	return ret;
}
int getKthFromEnd(Node* head, int k) {
	Node* fast = head;
	Node* slow = head;
    for (int i = 0; i < k; ++i) {
        if (fast == nullptr) return -100;
        fast = fast->next;
    }
    while (fast != nullptr) {
        fast = fast->next;
        slow = slow->next;
    }
    return slow->val;
}
